package com.currency.mall.ui.main.inquiry;

import com.currency.mall.Constant;
import com.currency.mall.entity.CollectShareId;
import com.currency.mall.entity.SecurityBean;
import com.currency.mall.entity.SharesBean;
import com.currency.mall.netWork.NetWork;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;
import java.util.HashMap;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/26.
 */

public class InquiryPresent implements InquiryContract.Presenter {
    private InquiryContract.View mView;

    public InquiryPresent(InquiryContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void getBondInfo(final int page, final boolean isTimer) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("pageNum", page);
        if(isTimer){
            hashMap.put("pageSize", page*50);
        }else{
            hashMap.put("pageSize", 50);
        }

        NetWork.getSharesApi().bondInfo(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<SharesBean>bindToLife())
                .subscribe(new Observer<SharesBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(SharesBean collectSharesBean) {
                        mView.finishRefresh();
                        if (collectSharesBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            if (page == 1) {
                                mView.setSharesData(collectSharesBean.getShares(), false,isTimer);
                            }else {
                                mView.setSharesData(collectSharesBean.getShares(), true,isTimer);
                            }

                        } else if (collectSharesBean.getCode().equals("9112")) {
                            PreferenceUtils.clearLoginInfo();
                            mView.toLogin();
                        } else {
                            ToastUitl.show(collectSharesBean.getMsg(), 200);
                        }
                    }
                });
    }

    @Override
    public void collectSharesInfo(String sharesId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sharesId", sharesId);
        NetWork.getSharesApi().collectSharesInfo(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.finishRefresh();
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("收藏成功", 200);
                            mView.refresh();
                        }else if(responseBean.getCode().equals("9112")){
                            PreferenceUtils.clearLoginInfo();
                            mView.toLogin();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }

    @Override
    public void cancelCollectShares(String sharesId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sharesId", sharesId);
        NetWork.getSharesApi().cancelCollectShares(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.finishRefresh();
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("取消收藏", 200);
                            mView.refresh();
                        }else if(responseBean.getCode().equals("9112")){
                            PreferenceUtils.clearLoginInfo();
                            mView.toLogin();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }

    @Override
    public void securityInfo() {
        NetWork.getSharesApi().securityInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<SecurityBean>bindToLife())
                .subscribe(new Observer<SecurityBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(SecurityBean securityBean) {
                        mView.finishRefresh();
                        if (securityBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            mView.setSecurityData(securityBean.getBonds());
                        }else if(securityBean.getCode().equals("9112")){

                        } else {
                            ToastUitl.show(securityBean.getMsg(), 200);
                        }

                    }
                });
    }

    @Override
    public void getCollectSharesId(final boolean collect,final boolean isTimer) {
        NetWork.getSharesApi().getCollectShareId()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<CollectShareId>bindToLife())
                .subscribe(new Observer<CollectShareId>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.collectSharesId(new String[0],collect,isTimer);
                    }

                    @Override
                    public void onNext(CollectShareId collectShareId) {
                        if (collectShareId.getCode().equals(Constant.SUCCESS_CODE)) {
                            int collectShareIds[] = collectShareId.getCollectShareIds();
                            String str[] = new String[collectShareIds.length];
                            for (int i = 0; i < collectShareIds.length; i++) {
                                str[i] = collectShareIds[i] + "";
                            }
                            mView.collectSharesId(str,collect,isTimer);
                        }else if(collectShareId.getCode().equals("9112")){
                            PreferenceUtils.clearLoginInfo();
                        } else {
                            ToastUitl.show(collectShareId.getMsg(), 200);
                        }

                    }
                });
    }
}
